const foodTypeFacade = require('../facade/foodType.server.facade.js');
var eventproxy = require('eventproxy');
const _ = require('lodash');
var ep = require('../util/event_proxy');

module.exports = {
    addFoodType: addFoodType,
    getFoodTypeByFoodTypeId:getFoodTypeByFoodTypeId,
    getAllFoodType:getAllFoodType

}


function addFoodType(req, res, next) {

    let params = req.params;
    let foodTypeName = params.foodTypeName;

    if (_.isEmpty(foodTypeName)) {
        return ep.emit('err', res, 422, "FoodType name is null");
    }

    foodTypeFacade.getFoodTypeByFoodTypeName(foodTypeName, (error, result) => {

        if (result) {
            return ep.emit('err', res, 422, 'FoodType is exit');
        }

        foodTypeFacade.addFoodType(foodTypeName, (error, foodType) => {

            if (error) {
                return next(error);
            }

            return ep.emit('success', res, result, "Add foodType Success!");

        });

    });

}

function getFoodTypeByFoodTypeId(req, res, next) {
    let params = req.params;
    let foodTypeId = params.foodTypeId;

    if (_.isEmpty(foodTypeId)) {
        return ep.emit('err', res, 422, "FoodType id is null");
    }

    foodTypeFacade.getFoodTypeByFoodTypeId(foodTypeId, (error, result) => {
        if (error) {
            return next(error);
        }

        return ep.emit('success', res, result, "Get Success!");

    });

}

function getAllFoodType(req, res, next) {

    foodTypeFacade.getAllFoodType((error, result) => {

        if (error) {
            return next(error);
        }

        return ep.emit('success', res, result, "Get Success!");

    });

}
